package com.sunnada.edu.business.contracTemplate.controller;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.sunnada.edu.business.attendance.bean.AttenRecord;
import com.sunnada.edu.business.attendance.bean.Attendance;
import com.sunnada.edu.business.contracTemplate.bean.ContracTemplate;
import com.sunnada.edu.business.contracTemplate.service.ContracTemplateService;
import com.sunnada.edu.system.pub.common.BasicController;
import com.sunnada.edu.system.pub.dao.Page;
import com.sunnada.edu.system.pub.filter.XssHttpServletRequestWrapper;
import com.sunnada.edu.system.pub.service.PubMethods;
import com.sunnada.edu.system.pub.util.Office2PDF;
import com.sunnada.edu.system.pub.util.Utils;

/***
 * explain 合同模板
 * 
 * @author zhenglx
 */
@Controller
@RequestMapping("/contracTemplateController")
public class ContracTemplateController extends BasicController {
	@Resource
	ContracTemplateService contracTemplateService;

	/** 合同模板首页 */
	@RequestMapping("/contracTemplate")
	public String contracTemplate(HttpServletRequest request) {
		String curr = request.getParameter("curr");
		request.setAttribute("curr", curr);
		// 当前路径
		String moduleId = request.getParameter("moduleId");
		request.setAttribute("pathValue", PubMethods.getPathValue(moduleId));
		return "business/contracTemplate/contracTemplate";
	}
	/** 合同模板查询 */
	@RequestMapping("/contracTemplateSearch")
	public void contracTemplateSearch(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, ContracTemplate.class, params);
		Page page = new Page();
		page.setRowCount(contracTemplateService.getContracTemplateCots(params));
		page.setPageMap(request, page, params);
		List<?> userList = contracTemplateService.findContracTemplate(params);
		JSONObject json = new JSONObject();
		json.put("list", userList);
		json.put("params", params);
		printInfo(response, json);
	}
	/** 合同模板新增*/
	@RequestMapping("/contracTemplateAdd")
	public String contracTemplateAdd(HttpServletRequest request) {
		return "business/contracTemplate/contracTemplateAdd";
	}
	
	
	
	/** 合同模板修改页面 */
	@RequestMapping("/contracTemplateUpdate")
	public String contracTemplateUpdate(HttpServletRequest request) {
		request.setAttribute("recId", request.getParameter("recId"));
		return "business/contracTemplate/contracTemplateUpdate";
	}

	/** 合同模板查询 */
	@RequestMapping("/contracTemplateById")
	public void contracTemplateById(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Attendance.class, params);
		Map userList = contracTemplateService.contracTemplateById(params);
		JSONObject json = new JSONObject();
		json.put("list", userList);
		printInfo(response, json);
	}

	/** 合同模板保存功能 */
	@RequestMapping("/contracTemplateSave")
	public void contracTemplateSave(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		HttpServletRequest re=XssHttpServletRequestWrapper.getOrgRequest(request);
		setReqParams(re, ContracTemplate.class, params);
		String recId = params.get("recId");
		HashMap<String, String> param = new HashMap<String, String>();
		param.put("ctName", params.get("ctName"));
		Map l = contracTemplateService.contracTemplateById(param);
		msg = "1";
		if(Utils.isNotNull(l)){
			if(Utils.isNotNull(recId)){
				HashMap<String, String> param1 = new HashMap<String, String>();
				param1.put("recId", recId);
				Map li = contracTemplateService.contracTemplateById(param1);
				if(li.get("NAME").equals(l.get("NAME"))){
					msg = "1";
				}else{
					msg = "2";
				}
			}else{
				msg = "2";
			}
		}
		if(msg.equals("1")){
			if(Utils.isNotNull(recId)){
				msg = contracTemplateService.contracTemplateUpdate(params);
			}else{
			    recId = UUID.randomUUID().toString();
				params.put("recId", recId);
				msg = contracTemplateService.contracTemplateAdd(params);
			}
		}
	    printInfo(response, msg);
	}
	/** 合同模板删除功能 */
	@RequestMapping("/contracTemplateDel")
	public void contracTemplateDel(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds", Utils.getSqlSplitStr(recIds));
		msg = contracTemplateService.contracTemplateDelete(params);
		printInfo(response, msg);
	}
	
	
	/** 合同模板生成pdf */
	@RequestMapping("/contracTemplatepdf")
	public void contracTemplatepdf(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		HttpServletRequest re=XssHttpServletRequestWrapper.getOrgRequest(request);
		setReqParams(re, ContracTemplate.class, params);
		msg = "1";
		String filePath = "pages/business/contracTemplate/index.html" ;
		String dest = "d:\\test.pdf";
		String tempPath = "pages/business/contracTemplate/";
		String content = params.get("ctContent");
		Office2PDF.officetoPDF(filePath, dest,tempPath,content,request);
		printInfo(response, msg);
	}

}
